package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.OrderSearch;
import com.jiai.cloud.entity.TOrder;
import com.jiai.cloud.sys.service.ITOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 佳爱科技
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/order")
public class TOrderController {
    @Autowired
    private ITOrderService itOrderService;

    //增
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object addOrder(@RequestBody TOrder tOrder) throws ServiceException {
        try {
            itOrderService.insert(tOrder);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //删
    @RequestMapping(value = "/deleteId/{orderNo}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try {

            itOrderService.deleteById(orderNo);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    //查询所有的
    @RequestMapping(value = "/findByAll", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findByAll(OrderSearch search) throws ServiceException {
        try {
            Page page=search.getPagePlus();
            EntityWrapper wrapper=new EntityWrapper();
            if(search.getState()!=null){
                wrapper.eq("state",search.getState());
            }
            if(!StringUtils.isEmpty(search.getOrderNo())){
                wrapper.like("orderNo",search.getOrderNo());
            }
            itOrderService.selectPage(page, wrapper);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //根据订单查找
    @RequestMapping(value = "/findByID/{orderNo}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findByid(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try {
            itOrderService.selectById(orderNo);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //根据状态查找
    @RequestMapping(value = "/findByState/{state}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findByState(@PathVariable("state") Integer state) throws ServiceException {
        try {
            itOrderService.selectById(state);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }


    //改(有就改，没有就插入)
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TOrder tOrder) throws ServiceException {
        try {
            itOrderService.insertOrUpdate(tOrder);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }

    //通过改orderNo改状态
    @RequestMapping(value = "/changeState/{orderNo}/{state}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changState(@PathVariable("orderNo") String orderNo,@PathVariable("state") Integer state) throws ServiceException {
        try {
            TOrder tOrder1 = new TOrder();
            tOrder1.setOrderNo(orderNo);
            tOrder1.setState(state+1);
            itOrderService.updateById(tOrder1);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }

    //连表查找 根据订单详细表和会员表中的内容 查出订单表中的 交易状态和会员名字
    @RequestMapping(value = "/selectMember", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object selectMember(OrderSearch search) throws ServiceException {
        try {
            Page page=search.getPagePlus();
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getStartTime())){
                wrapper.between("o.orderTime",search.getStartTime(),search.getEndTime());
            }

            itOrderService.selectMember(page, wrapper);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/countNumber", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object countNumber() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itOrderService.countNumber());
        } catch (Exception e) {
            throw new ServiceException("统计失敗", e.getMessage());
        }
    }


}
